/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import system.*;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import system.MovieHandler;
/**
 *
 * @author Thommie
 */
public class LowerPanel extends JPanel implements ActionListener {

    private JScrollPane scrollPane;
    private JScrollPane scrollPane2;
    private javax.swing.JTextArea textPostArea;
    private javax.swing.JTextArea textArea;
    private JButton postButton = new JButton("post");
    private JPanel top;
    private JPanel bot;
    MovieHandler mHandler;

    // ButtonListener buttonListener = new ButtonListener();
    public LowerPanel(MovieHandler mHandler) {

        this.mHandler = mHandler;
        top = new JPanel();
        bot = new JPanel();
        this.setLayout(new BorderLayout());
        this.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        textPostArea = new javax.swing.JTextArea();
        textArea = new javax.swing.JTextArea();
        textArea.setSelectionColor(Color.GREEN);
        textPostArea.setSelectionColor(Color.GREEN);
        // bot
        bot.add(textPostArea);
        bot.add(postButton);
        textPostArea.setColumns(25);
        textPostArea.setRows(6);
        scrollPane = new JScrollPane(textPostArea);
        bot.add(scrollPane);
        //top
        top.add(textArea);
        textArea.setColumns(40);
        textArea.setRows(8);
        scrollPane2 = new JScrollPane(textArea);
        top.add(scrollPane2);
        textArea.setEditable(false);
        // textPostArea.setSelectedTextColor(Color.GREEN);
        // Add the scroll pane to the panel.
        this.add(bot, BorderLayout.SOUTH);  // blir utdragen, liten om south..
        this.add(top, BorderLayout.CENTER);
 
        postButton.addActionListener(this);
        postButton.setMnemonic(KeyEvent.VK_ENTER);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == postButton) {
            //textArea.append(test + "\n\n");
            mHandler.addComment(textPostArea.getText());
            textPostArea.setText("");
            setTextArea();
        }
    }

    private String getCommentsFormatedForTextArea() {
        String ret = "";

        system.Comment [] com = mHandler.getCommentsForSelectedMovieID();


        for (int i = 0; i <= com.length-1 && com[i] != null; i++){
            if (com[i] != null){
                if (com[i].getUserid() != -1){
                    ret += mHandler.geUserNameForUserID(com[i].getUserid()) + ", " + com[i].getDatetime();
                    ret += "\n";
                    ret += com[i].getComment();
                    ret += "\n\n";
                } else {
                    ret += com[i].getComment();
                    ret += "\n\n";
                }
            }
        }

        return ret;
    }

    /**
     * @param textArea the textArea to set
     */
    public void setTextArea() {
        this.textArea.setText("");
        this.textArea.append(getCommentsFormatedForTextArea());
    }
}
